package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
;

/**
 * @program: jxc
 * @description:
 * @author: SXC
 * @create: 2021-09-04 11:38
 **/
@RestController
public class SupplierController{
    @Autowired
    private SupplierService supplierService;

    /**
     * 请求URL：http://localhost:8080/supplier/list
     * 请求参数：Integer page（当前页数）, Integer rows（每页显示的记录数）, String supplierName
     * 请求方式：POST
     * 返回值类型： JSON
     * 返回值：Map<String,Object>
     * Response Example Value：
     */

    /**
     * 分页查询供应商
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @PostMapping(value = "supplier/list")
    public Map<String,Object> getSupplierList(Integer page, Integer rows, String supplierName){
        //获取数据
        List<Supplier> list = supplierService.getSupplierList(page,rows,supplierName);
        //获取条数
        int total = supplierService.selectCount();
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        map.put("total",total);
        return map;
    }


    /**
     * 请求URL：http://localhost:8080/supplier/save?supplierId=1
     * 请求参数：Supplier supplier
     * 请求方式：POST
     * 返回值类型： JSON
     * 返回值：ServiceVO
     */
    @PostMapping(value = "supplier/save")
    public  ServiceVO save(Supplier supplier){
        supplierService.saveOrUpdateSupplier(supplier);
        return new ServiceVO(100,"请求成功",null);
    }



    /**
     * 请求URL：http://localhost:8080/supplier/delete
     * 删除供应商（支持批量删除）
     * @param ids
     * @return
     */
    @PostMapping(value = "supplier/delete")
    public ServiceVO delete(String[] ids){
        supplierService.deleteList(ids);
        return  new ServiceVO(100,"请求成功",null);
    }

}
